//
//  Data.m
//  網易
//
//  Created by 赵天宇 on 16/9/7.
//  Copyright © 2016年 赵天宇. All rights reserved.
//

#import "Data.h"
#import "ASIHTTPRequest.h"
#import "AppDelegate.h"
static Data *dd=nil;
static  dispatch_once_t  predicate;
@implementation Data{
    AppDelegate *appDelegate;
}
+(id)sharedData{
    dispatch_once(&predicate, ^{
        if(dd==nil){
            dd=[[Data alloc]init];
        }
    });
    return dd;
}
-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {
//    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
//    NSURL *url = [NSURL URLWithString: urlStr];
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
//    [request setHTTPMethod: @"GET"];
//    [request addValue: @"44149ec2b7a1fc46f8e9102a4cd8061a" forHTTPHeaderField: @"apikey"];
//    [NSURLConnection sendAsynchronousRequest: request
//                                       queue: [NSOperationQueue mainQueue]
//                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
//                               if (error) {
//                               } else {
//                                          NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//                                   [[NSNotificationCenter defaultCenter]postNotificationName:@"hello" object:dic];
//                               }
//                           }];
        NSString *urlStr = [[NSString alloc]initWithFormat: @"%@&%@", httpUrl, HttpArg];
        NSURL *url = [NSURL URLWithString: urlStr];
        ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
        [request addRequestHeader:@"key" value:@"158cc0978fcc0b6a04f8ccf60f56ff8d"];
        ASIHTTPRequest *newRequst = [[ASIHTTPRequest alloc]init];
        newRequst =request;
        [request setCompletionBlock:^{
            NSData *data = newRequst.responseData;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"hello" object:dic];
        }];
        [request startSynchronous];
    
//-(void)getData:(NSString *)url{
//    NSURL *uu= [NSURL URLWithString:url];
//    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:uu];
//    [request addRequestHeader:@"apikey" value:@"44149ec2b7a1fc46f8e9102a4cd8061a"];
//    ASIHTTPRequest *newRequst = [[ASIHTTPRequest alloc]init];
//    newRequst =request;
//    [request setCompletionBlock:^{
//        NSData *data = newRequst.responseData;
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"hello" object:dic];
//    }];
//    [request startAsynchronous];
//    //获取全局变量
////    appDelegate= [[UIApplication sharedApplication] delegate];
////    //设置缓存方式
////    [request setDownloadCache:appDelegate.myCache];
////    //设置缓存数据存储策略，这里采取的是如果无更新或无法联网就读取缓存数据
////    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
////    request.delegate = self;
//}
}
@end
